package com.webrtc.demo

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import com.webrtc.demo.call.*
import com.webrtc.demo.util.ContactsUtils
import kotlinx.android.synthetic.main.activity_chat_group.*
import org.webrtc.Logging

class ChatGroupActivity : AppCompatActivity() {

    companion object {
        const val TAG = "ChatGroupActivity"

        const val TO_NAMES = "TO_NAMES"
        const val ROOM_ID = "ROOM_ID"
        const val CALLER_ID = "CALLER_ID"

        fun launch(
            context: Context,
            toNames: ArrayList<String>,
            roomId: String?,
            callerUid: String?
        ) {
            context.startActivity(
                Intent(
                    context,
                    ChatGroupActivity::class.java
                ).putStringArrayListExtra(
                    TO_NAMES,
                    toNames
                )
                    .putExtra(ROOM_ID, roomId)
                    .putExtra(CALLER_ID, callerUid)
            )
        }
    }

    private val toNames by lazy { intent.getStringArrayListExtra(TO_NAMES) }
    private val roomId by lazy { intent.getStringExtra(ROOM_ID) } // roomId 如果有传，说明自己是被呼叫方；否则自己是发起人
    private val callerUid by lazy { intent.getStringExtra(CALLER_ID) }

    private val myId by lazy { ContactsUtils.getMyUid(this) }

    private val mAdapter by lazy {
        ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1)
    }

    private lateinit var chatHelper: MultiChatHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat_group)

        listView.adapter = mAdapter

        // 发送消息
        btn_send.setOnClickListener {

            val msg = RTCSignalClient.getMyId() + ": " + et_msg.text.toString()
            if (msg.isEmpty()) {
                return@setOnClickListener
            }
            val send = chatHelper.send(msg)
//            if (send) {
            mAdapter.add(msg)
            et_msg.text = null
//            } else {
//                mAdapter.add("发送消息失败")
//            }
        }

        chatHelper =
            MultiChatHelper(
                myId,
                toNames,
                roomId,
                callerUid,
                object : MultiChatHelper.MutiChatUserListener {

                    override fun onUserDeny(uid: String) {
                        Log.d(TAG, "$uid 拒绝了你的连接请求")
//                        logOnUI("$uid 拒绝了你的连接请求")
                    }

                    override fun onDataReceived(uid: String, data: String) {
                        runOnUiThread {
                            mAdapter.add("你收到了：$data")
                        }
                    }
                })


        // NOTE: this _must_ happen while PeerConnectionFactory is alive!
        Logging.enableLogToDebugOutput(Logging.Severity.LS_VERBOSE)
    }

    override fun onDestroy() {
        super.onDestroy()
        chatHelper.close()
    }

}
